﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Wangzeran.CodeMaker.Core.Model;
using Wangzeran.CodeMaker.Core.Template;
using Wangzeran.CodeMaker.GUI.Codes;
using Wangzeran.CodeMaker.GUI.Utils;
using System.Diagnostics;

namespace Wangzeran.CodeMaker.GUI.Forms
{
    /// <summary>
    /// 模板管理界面
    /// </summary>
    public partial class TemplateForm : Form
    {

        private XmlMgt opXml = new XmlMgt();

        public TemplateForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 加载窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TemplateForm_Load(object sender, EventArgs e)
        {
            // 加载模板
            IList<TemplateFileModal> lstTemplates = new List<TemplateFileModal>();
            foreach (string language in TemplateFiles.Files.Keys)
            {
                foreach (TemplateFileModal modal in TemplateFiles.Files[language])
                {
                    lstTemplates.Add(modal);
                }
            }
            foreach (TemplateFileModal template in lstTemplates)
            {
                ListViewItem item = new ListViewItem();
                item.Text = template.TemplateName;
                item.SubItems.Add(template.Language);
                item.SubItems.Add(template.FilePath);
                item.SubItems.Add(template.Author);
                item.SubItems.Add(template.Version);
                item.ImageIndex = 0;
                lvTemplates.Items.Add(item);
            }
            tsslblCount.Text = lstTemplates.Count.ToString();
        }

        /// <summary>
        /// 添加新模板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnAddNew_Click(object sender, EventArgs e)
        {
            MessageHelper.ShowWarning("提示", "暂不支持该功能");
        }

        /// <summary>
        /// 编辑模板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnEdit_Click(object sender, EventArgs e)
        {
            if (lvTemplates.SelectedItems.Count == 0)
            {
                MessageHelper.ShowWarning("提示", "请先选择模板");
                return;
            }
            ListViewItem item = lvTemplates.SelectedItems[0];
            string filePath = item.SubItems[2].Text;
            Process.Start("notepad.exe", Application.StartupPath + "\\" + filePath);
        }

        private void lvTemplates_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            tsbtnEdit_Click(null, null);
        }
    }
}
